package top.codedance.iotp.serialPortSDK.device.gld;

import top.codedance.iotp.serialPortSDK.command.YbdCommand;
import top.codedance.iotp.serialPortSDK.result.YouBiDeAlarm;
import top.codedance.iotp.serialPortSDK.util.FloatUtil;
import top.codedance.iotp.serialPortSDK.util.HexUtil;

import java.util.*;

/**
 * 设备类型：测漏检测仪
 * 品牌：优必得
 * 型号：KP315AU
 * @author ChenPeng
 * @Description
 * @date 2023/4/10 10:19
 */
public class YouBiDe_Gld implements Gld {

    public static List<YouBiDeAlarm> list = new ArrayList<>();

    public static Gld getInstance() {
        return new YouBiDe_Gld();
    }

    @Override
    public void parse(String result) {
        if (null == result || result.isEmpty()) return;
        String head = result.substring(0, 2);
        if (head.equals("06")) {
            String packs = result.substring(2);
            List<String> resultList = new ArrayList<>();
            String s;
            for (int i = 0; i < packs.length(); i += 16) {
                if(packs.length() <= i + 16){
                    break;
                }
                s = packs.substring(i, i + 16);
                if (s.matches("[fF]{16}")) {
                    break;
                }
                resultList.add(s);
            }
            // 单条数据返回
            if (resultList.size() == 1) {
                // 测试调用16进制返回16进制,调用地址：sdk.StandandTestSdk.execCom
                List<String> newModelData = HexUtil.hexStringToListString(resultList.get(0), 2);
                // 06 CH YY MM DD HH MM SS DD
                YouBiDeAlarm alarm = new YouBiDeAlarm();
                // CH 通道数
                alarm.setDeviceChannel(newModelData.get(0));
                // YY MM DD HH MM SS 年月日时分秒
                StringBuilder sb = new StringBuilder();
                sb.append(newModelData.get(1))
                        .append(newModelData.get(2))
                        .append(newModelData.get(3))
                        .append(newModelData.get(4))
                        .append(newModelData.get(5))
                        .append(newModelData.get(6));
                alarm.setAlarmStartTime(FloatUtil.timeToData(sb.toString()));
                // DD 事件类型
                alarm.setDeviceStatus(HexUtil.HexToInt(newModelData.get(7)));
                list.add(alarm);
            } else {
                for (String item : resultList) {
                    YouBiDeAlarm alarm = new YouBiDeAlarm();
                    List<String> newModelData = HexUtil.hexStringToListString(item, 2);
                    alarm.setDeviceChannel(String.valueOf(HexUtil.HexToInt(newModelData.get(0))));// CH 通道数
                    // YY MM DD HH MM SS 年月日时分秒
                    StringBuilder sb = new StringBuilder();
                    sb.append(newModelData.get(1))
                            .append(newModelData.get(2))
                            .append(newModelData.get(3))
                            .append(newModelData.get(4))
                            .append(newModelData.get(5))
                            .append(newModelData.get(6));
                    alarm.setAlarmStartTime(FloatUtil.timeToData(sb.toString()));
                    // DD 事件类型
                    alarm.setDeviceStatus(HexUtil.HexToInt(newModelData.get(7)));
                    list.add(alarm);
                }
            }
        }
    }

    @Override
    public String genCommandAlarm() {
        return YbdCommand.ALARM_CODE;
    }

    @Override
    public List<YouBiDeAlarm> getAlarmInfos() {
        return list.isEmpty() ? Collections.emptyList() : list;
    }

    /*
     * 根据事件类型返回事件消息
     * @param value
     * @return
     */
    public static String getAlarmDesc(Integer p) {
        String param = f(Integer.toBinaryString(p.intValue()));
        Map<String, String> eventAll = new HashMap<>();
        eventAll.put("00000001", "传感器断路"); // 1
        eventAll.put("00000010", "传感器短路"); // 2
        eventAll.put("00000100", "高限报警");   // 4
        eventAll.put("00001000", "低限报警");   // 8
        eventAll.put("00010000", "数据采集");   // 16
        eventAll.put("00100000", "设置时间");   // 32
        eventAll.put("01000000", "重新启动");   // 64
        eventAll.put("10000000", "清存储器");   // 128
        return eventAll.get(param);
    }

    /**
     * 二进制数据补位（8位）
     * @Description
     * @Param v
     * @return java.lang.String
     * @author ChenPeng
     * @date 2023/4/28 10:00
     */
    private static String f(String v) {
        int i = 8 - v.length();
        String t = "";
        for (int j = 1; j <= i; j++) {
            t += "0";
        }
        return t + v;
    }
}
